// new 运算符用来创建用户自定义的对象类型的实例或者具有构造函数的内置对象的实例。

// 实现要点：

// new 会产生一个新对象；
// 新对象需要能够访问到构造函数的属性，所以需要重新指定它的原型；
// 构造函数可能会显示返回；
function Animal (type) {
  this.type = type
  // return {'this':'你好'}
}
Animal.prototype.say = function () {
  console.log('狗是' + this.type + '类型');
}
function mockNew () {
  // const constructor = [].shift.call(arguments)
  // let obj = {}
  // obj.__proto__ = constructor.prototype
  // // console.log(arguments);
  // let ret = constructor.apply(obj, arguments)
  // return ret instanceof Object ? ret : obj
  let args = [...arguments]
  let constructor = args.shift() // 弹出构造函数
  let obj = {}
  obj.__proto__ = constructor.prototype
  let ret = constructor.apply(obj, args)
  return ret instanceof Object ? ret : obj
}
// let dog = new Animal('dog')
let dog = mockNew(Animal, 'dog')
console.log(dog.type);
dog.say()
// console.log(dog);
// function MockNew(){
//   let constructor = [].shift.call(arguments)
//   let obj = {}
//   obj.__proto__ = constructor.prototype
//   let ret = constructor.apply(obj,arguments)
//   // return typeof ret === "object" ? ret : obj
//   return ret instanceof Object ? ret : obj
// }

// function Animal1(name){
//   this.name = name
// }
// Animal1.prototype.say = function(){
//   console.log('my name is ' + this.name);
// }
// function newNew(fun){
//   let constructor = fun
//   let obj = new Object()
//   obj.__proto__ = fun.prototype
//   let args = [...arguments].slice(1)
//   console.log(args);
//   let result = constructor.call(obj,...args)
//   return typeof result === 'object' ? result : obj
// }
// let dog1 = newNew(Animal1,'小米')
// console.log(dog1);
// dog1.say()
function newObj (name) {
  this.name = name
  let data = {
    name: '张三'
  }
  return data
}
let obj1 = mockNew(newObj, '方现伟')
console.log(obj1);